package com.taorenqi.business.fragment.mine;


import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.taorenqi.business.R;
import com.taorenqi.business.activity.PermissionsActivity;
import com.taorenqi.business.app.MyApp;
import com.taorenqi.business.fragment.base.BaseBackFragment;
import com.taorenqi.business.utils.CommonUtil;
import com.taorenqi.business.utils.StringUtils;
import com.taorenqi.business.utils.ToastUtil;
import com.taorenqi.business.widget.statusview.StatusView;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;

import java.lang.ref.WeakReference;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.yokeyword.fragmentation.SupportActivity;
import me.yokeyword.fragmentation.SupportFragment;

import static android.content.Context.CLIPBOARD_SERVICE;

/**
 * 邀请返利
 * A simple {@link Fragment} subclass.
 */
public class RebateFragment extends BaseBackFragment {


    @Bind(R.id.rebate_friends_number)
    TextView rebateFriendsNumber;
    @Bind(R.id.rebate_ll_friends)
    LinearLayout rebateLlFriends;
    @Bind(R.id.rebate_reward_number)
    TextView rebateRewardNumber;
    @Bind(R.id.rebate_ll_reward)
    LinearLayout rebateLlReward;
    @Bind(R.id.rebate_qq)
    TextView rebateQq;
    @Bind(R.id.status_view)
    StatusView statusView;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.rebate_copy)
    Button rebateCopy;
    @Bind(R.id.rebate_share)
    Button rebateShare;
    @Bind(R.id.rebate_code)
    TextView rebateCode;

    private UMShareListener mShareListener;
    private ShareAction mShareAction;

    public static RebateFragment newInstance() {
        // Required empty public constructor
        RebateFragment fragment = new RebateFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_rebate, container, false);
        ButterKnife.bind(this, view);
        if (MyApp.getApp().getPermissionsChecker().lacksPermissions(MyApp.getApp().PHONE_WRITE_READ)) {
            PermissionsActivity.startActivityForResult(_mActivity, MyApp.getApp().REQUEST_CODE, MyApp.getApp().PHONE_WRITE_READ);
        }
        toolbar.setTitle("邀请返利");
        initToolbarNav(toolbar);
        initShare();

        return attachToSwipeBack(view);
    }

    private void initShare() {

        mShareListener = new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {

            }

            @Override
            public void onResult(SHARE_MEDIA platform) {
                Log.d("plat", "platform" + platform);

                Toast.makeText(_mActivity, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onError(SHARE_MEDIA platform, Throwable t) {
                Toast.makeText(_mActivity, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
                if (t != null) {
                    Log.d("throw", "throw:" + t.getMessage());
                }
            }

            @Override
            public void onCancel(SHARE_MEDIA platform) {
                Toast.makeText(_mActivity, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
            }
        };
        mShareAction = new ShareAction(_mActivity).setDisplayList(
                SHARE_MEDIA.WEIXIN, SHARE_MEDIA.QQ)
                .addButton("umeng_sharebutton_copyurl", "umeng_sharebutton_copyurl", "umeng_socialize_copyurl", "umeng_socialize_copyurl")
                .setShareboardclickCallback(new ShareBoardlistener() {
                    @Override
                    public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
                        if (snsPlatform.mShowWord.equals("umeng_sharebutton_copyurl")) {
                            Toast.makeText(_mActivity, "复制链接成功", Toast.LENGTH_LONG).show();

                        } else {
                            UMWeb web = new UMWeb("https://www.baidu.com");
                            web.setTitle("91人气-百万电商卖家好帮手");
                            web.setDescription("专属无线优化大师，打造无线趋势新方向。邀请码：");
                            web.setThumb(new UMImage(_mActivity, R.drawable.app_icon));
                            new ShareAction(_mActivity).withMedia(web)
                                    .setPlatform(share_media)
                                    .setCallback(mShareListener)
                                    .share();
                        }
                    }
                })
                .setCallback(mShareListener)
        ;

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.rebate_qq, R.id.rebate_copy, R.id.rebate_ll_friends, R.id.rebate_ll_reward, R.id.rebate_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rebate_copy:
                ClipboardManager cm = (ClipboardManager) _mActivity.getSystemService(CLIPBOARD_SERVICE);
                if (!StringUtils.isEmpty(rebateCode.getText().toString())) {
                    cm.setText(rebateCode.getText().toString());
                    ToastUtil.showToast(_mActivity, "复制邀请码成功");
                }
                break;
            case R.id.rebate_ll_friends:
                break;
            case R.id.rebate_ll_reward:
                break;
            case R.id.rebate_share:
                mShareAction.open();
                break;
            case R.id.rebate_qq:
                if (isQQClientAvailable(_mActivity)) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://wpa.b.qq.com/cgi/wpa.php?ln=2&uin=4001176017")));
                } else {
                    ToastUtil.showToast(_mActivity, "请安装QQ");
                }
                break;
        }
    }



    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }

}
